jQuery(function() {
    jQuery("#reload-btn").click(function() {
        jQuery(this).addClass("disabled");
        window.location.reload();
    });

    jQuery("#preview-btn").click(function() {
        jQuery(this).addClass("disabled");
        var archiveId = PageContext.getLong("archiveId");
        window.location.href = PageContext.getContextPath() + "/note/archive/display.html?archiveId=" + archiveId;
    });

    jQuery("#save-btn").click(function() {
        var src = jQuery(this);

        if(src.hasClass("disabled")) {
            return;
        }

        src.addClass("disabled");

        var editor = Marked.getInstance(jQuery("textarea[name=content]").get(0));
        var archiveId = PageContext.getLong("archiveId");
        var title = jQuery("#title").val();
        var mimeType = jQuery("textarea[name=content]").attr("mimeType");
        var content = editor.getContent();
        var pageNum = PageContext.getInt("pageNum");
        var version = PageContext.getLong("version");

        var params = {
            "archiveId": archiveId,
            "title": title,
            "mimeType": PageContext.getAttribute("editType"),
            "content": content,
            "pageNum": pageNum,
            "version": version
        };

        jQuery.ajax({
            "type": "post",
            "url": PageContext.getContextPath() + "/note/archive/save.html",
            "data": jQuery.param(params, true),
            "dataType": "json",
            "error": function() {
                src.removeClass("disabled");
                alert("系统错误，请稍后再试！");
            },
            "success": function(result) {
                src.removeClass("disabled");
                Response.success(result, function(){
                    window.location.reload();
                });
            }
        });
    });
});

/**
 * 初始化编辑器
 */
jQuery(function() {
    var theme = PageContext.getAttribute("theme");
    var contextPath = PageContext.getContextPath();
    var textarea = jQuery("textarea[name=content]");
    textarea.marked();
});

/**
 * 上传支持
 */
jQuery(function() {
    /**
     * 注册上传组件
     */
    Marked.uploader = {
        /**
         * 当用户点击上传按钮的时候触发
         */
        open: function(callback) {
            var uploader = new com.skin.upload.FileUpload({
                "accept": "",
                "multiple": false,
                "dataType": "json",
                "error": function() {
                    alert("上传失败，请稍后再试！");
                },
                "select": function(files) {
                    var archiveId = PageContext.getLong("archiveId");
                    var url = PageContext.getContextPath() + "/note/upload.html?archiveId=" + archiveId;

                    for(var i = 0; i < files.length; i++) {
                        this.upload(url, "uploadFile", files[i], null);
                    }
                },
                "success": function(result) {
                    if(result == null || result.status != 200) {
                        alert("上传失败，请稍后再试！");
                        return;
                    }
                    if(callback != null) {
                        callback(result.value.filePath);
                    }
                }
            });

            /**
             * 打开选择文件对话框
             */
            uploader.open();
        },
        select: function(files, callback) {
            var uploader = new com.skin.upload.FileUpload({
                "accept": "",
                "multiple": false,
                "dataType": "json",
                "error": function() {
                    alert("上传失败，请稍后再试！");
                },
                "select": function(files) {
                },
                "success": function(result) {
                    if(result == null || result.status != 200) {
                        alert("上传失败，请稍后再试！");
                        return;
                    }
                    if(callback != null) {
                        callback(result.value.filePath);
                    }
                }
            });

            /**
             * 开始上传
             */
            var archiveId = PageContext.getLong("archiveId");
            var url = PageContext.getContextPath() + "/note/upload.html?archiveId=" + archiveId;

            for(var i = 0; i < files.length; i++) {
                uploader.upload(url, "uploadFile", files[i], null);
            }
        }
    };
});

